﻿CREATE PROCEDURE [acms].[File_Put]
	@Id int, 
	@FileData image,
	@Name varchar(5),
	@Size bigint,
	@FileType int,
	@ContentType varchar(50),
	@Extension varchar(5),
	@Width int,
	@Height int,
	@VersionId timestamp,
	@IdOut int output,
	@VersionIdOut timestamp output
AS
	if exists (Select null from File_View where Id = @Id) begin
		update Files
		set 
			FileData = @FileData,
			Name = @Name,
			Size = @Size,
			FileType = @FileType,
			ContentType = @ContentType,
			Extension = @Extension,
			Width = @Width,
			Height = @Height
		where Id = @Id
			and VersionId = @VersionID
		if @@Rowcount = 0
			return -100
		else
			set @IdOut = @Id
	end
	else if @Id is null or @Id = 0 begin
		insert Files
			(FileData, Name, Size, FileType, ContentType, Extension, Width, Height)
		values
			(@FileData, @Name, @Size, @FileType, @ContentType, @Extension, @Width, @Height)
		set @IdOut = @@Identity
	end
	else
		return -200
		
	select @VersionIdOut = VersionId From File_View Where Id = @IdOut
	
RETURN 0;